<?php

/**
 * inventario actions.
 *
 * @package    tvpv
 * @subpackage inventario
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 8507 2008-04-17 17:32:20Z fabien $
 */
class inventarioActions extends sfActions
{
  public function executeIndex($request)
  {
  	//$plist = $this->getRequestParameter("list","all");
  	$ppage = $this->getRequestParameter("page", 1);
    $this->articuloList = ArticuloPeer::getAll($ppage,$request->getParameter('sort'),$request->getParameter('find',null),$request->getParameter('find2',null),$request->getParameter('find3',null),$request->getParameter('find4',null));
    if($request->getParameter('find') == '')
      $request->setParameter('find',null);
    if($request->getParameter('find2') == '')
      $request->setParameter('find2',null);
    if($request->getParameter('find3') == '')
      $request->setParameter('find3',null);
    if($request->getParameter('find4') == '')
      $request->setParameter('find4',null);  	 
    $this->narticulos = ArticuloPeer::getTotal();
    //$this->setTemplate('index');
  }
  
  public function executeList()
  {
    $this->articuloList = ArticuloPeer::getAll();
  }

  public function executeCreate()
  {
    $this->form = new ArticuloForm();

    $this->setTemplate('edit');
  }

  public function executeEdit($request)
  {
    $this->form = new ArticuloForm(ArticuloPeer::retrieveByPk($request->getParameter('id')));
  }

  public function executeUpdate($request)
  {
    $this->forward404Unless($request->isMethod('post'));
    $this->form = new ArticuloForm(ArticuloPeer::retrieveByPk($request->getParameter('id')));

    $this->form->bind($request->getParameter('articulo'), $request->getFiles('articulo') );
    if ($this->form->isValid())
    {
    	$foto = $this->form->getValue('foto');
    	$this->logMessage("FOTO --->".$request->getParameter('foto'));
    	if ( is_object( $foto ) )
    	{
    	 $nombre = sha1( $foto->getOriginalName().$this->form->getValue('codigo') ).$foto->getExtension();
    	 //$nombre = sha1($foto . $this->form->getValue('codigo'));    	
    	 $foto->save(sfConfig::get('sf_root_dir').'/web/images/fotos/'.$nombre);
    	}
      $articulo = $this->form->save();
      $this->redirect('inventario/index');
    }

    $this->setTemplate('edit');
  }

  public function executeDelete($request)
  {
    $this->forward404Unless($articulo = ArticuloPeer::retrieveByPk($request->getParameter('id')));

    $articulo->delete();

    $this->redirect('inventario/index');
  }
}
